package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.CourseOrder;
import com.hualan.sfzy.service.course.CourseOrderService;
import com.hualan.sfzy.utils.Result;
import com.hualan.sfzy.vo.CourseOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {
    @Autowired
    private CourseOrderService courseOrderService;
    @PostMapping
    public Result add(@RequestBody CourseOrder courseOrder){
        int result = courseOrderService.add(courseOrder);
        if(result>0)
            return Result.success(result);
        return Result.error("订单添加失败");
    }
    @PostMapping("/addOrder")
    public Result addOrder(Integer courseId,Integer userId,double price){

        int result = courseOrderService.addNewOrder(courseId,userId,price);
        if(result>0)
            return Result.success(result);
        return Result.error("订单添加失败");
    }



    @GetMapping
    public Result find(){
        List<CourseOrder> courseOrders = courseOrderService.find();
        if(courseOrders!=null)
            return Result.success(courseOrders);
        return Result.error("订单查询失败");
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        CourseOrder courseOrder = courseOrderService.findById(id);
        if(courseOrder!=null)
            return Result.success(courseOrder);
        return Result.error("订单查询失败");
    }
    @PostMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestBody(required = false) CourseOrderVo courseOrder){
        return Result.success(courseOrderService.findPage(pageNum,pageSize,courseOrder));
    }
    @PutMapping
    public Result edit(@RequestBody CourseOrder courseOrder){
        int result = courseOrderService.edit(courseOrder);
        if(result>0)
            return Result.success(result);
        return Result.error("订单修改失败");
    }
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id){
        return Result.success(courseOrderService.remove(id));
    }
    @DeleteMapping("/batch")
    public Result removeBatch(Integer[] ids){
        System.out.println(ids);
        return Result.success(courseOrderService.removeBatch(ids));
    }
}
